package org.xx.armory.swing.components;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

public class MultiCastListener<T, E> {
    private final List<T> handlers;

    public MultiCastListener() {
        handlers = new ArrayList<>();
    }

    public final void invokeEach(
            E event,
            BiConsumer<T, E> consumer
    ) {
        this.handlers.forEach(handler -> consumer.accept(handler, event));
    }

    public void add(
            T listener
    ) {
        if (listener != null) {
            this.handlers.add(listener);
        }
    }

    public void remove(
            T listener
    ) {
        if (listener != null) {
            this.handlers.remove(listener);
        }
    }
}
